package com.eece496.Database;
import java.sql.*;
import java.util.ArrayList;

import com.eece496.Helpers.*;

public class Assignments extends Database{

	private int assignmentID;
	private String name;
	private int weight;
	private String courseNumber;
	private String sectionNumber; 
	
	public Assignments(){
	
		tableName = "Assignments";
	}
	
	public Assignments(int assignmentID, String name, int weight, String courseNumber, String sectionNumber)
	{
		tableName = "Assignments";
		this.assignmentID = assignmentID;
		this.name = name;
		this.weight = weight;
		this.courseNumber = courseNumber;
		this.sectionNumber = sectionNumber;
	}
	
	@Override
	public void Insert() throws SQLException {
	
		Statement stmt = connection.createStatement();
		
		String sqlString = "INSERT INTO " + tableName + " VALUES (" +
				assignmentID + ", " +			
				AddQuotes(name) + ", " +
				weight + ", " +
				AddQuotes(courseNumber) + ", " +
				AddQuotes(sectionNumber) +
				")";
		
	    stmt.executeUpdate(sqlString);
	    System.out.println(sqlString);
	}

	@Override
	public void Delete(String primaryKey) throws SQLException {
		
		Statement stmt = connection.createStatement();
		String sqlString = "DELETE from " + tableName + " WHERE assignmentID = " + AddQuotes(primaryKey);
		stmt.executeUpdate(sqlString);
		
	}

	@Override
	public void Display() throws SQLException {
		// TODO Auto-generated method stub
		
	}
	
	//Dont Delete- Added by Steven Chow
	public void DeleteCourseAssignments(String courseNumber, String sectionNumber) throws SQLException
	{
		Statement stmt = connection.createStatement();
		String sqlString = "DELETE from " + tableName + " WHERE courseNumber = " + AddQuotes(courseNumber)
				+ " and sectionNumber= " + AddQuotes(sectionNumber);
		stmt.executeUpdate(sqlString);
	}
	
	public ArrayList<AssignmentInfo> GetAssignmentInfo(String courseNumber, String sectionNumber) throws SQLException
	{
		ArrayList<AssignmentInfo> list = new ArrayList<AssignmentInfo>();
		Statement stmt = connection.createStatement();
		String sqlString = "select * from " + tableName + " where courseNumber = " + AddQuotes(courseNumber) + "AND sectionNumber = "
				+ AddQuotes(sectionNumber);
		
		ResultSet rs = stmt.executeQuery(sqlString);
		
		while(rs.next())//check if user exists in databse
		{
			AssignmentInfo info = new AssignmentInfo();
			info.name = rs.getString("name");
			info.weight = rs.getInt("weight");
			list.add(info);	
		}
		
		
		return list;
	}
}